package com.sharkburner.common.dao.paginator;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.vaadin.addons.lazyquerycontainer.LazyQueryContainer;
import org.vaadin.addons.lazyquerycontainer.Query;
import org.vaadin.addons.lazyquerycontainer.QueryDefinition;
import org.vaadin.addons.lazyquerycontainer.QueryFactory;

@Service
public class LazyPaginator {
    
    @Autowired
    private BeanFactory beanFactory;

    @Autowired
    protected PaginationFactory paginationFactory;

    public LazyQueryContainer build(final Object dataProvider) {
	
	return new LazyQueryContainer(new QueryFactory() {

	    @Override
	    public void setQueryDefinition(QueryDefinition arg0) {
		// TODO Auto-generated method stub

	    }

	    @Override
	    public Query constructQuery(Object[] arg0, boolean[] arg1) {
		PaginatorCriteria paginatorCriteria = beanFactory.getBean(PaginatorCriteria.class);
		paginatorCriteria.setFactory(paginationFactory
			.getFactory(dataProvider));

		return paginatorCriteria;
	    }
	}, false, 25);
    }
}
